﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CJJCH.Web.Manage
{
    public partial class OnlineList : System.Web.UI.Page
    {
        CJJCH.BLL.T_NET_Online bll_Online = new BLL.T_NET_Online();
        CJJCH.BLL.T_NET_MenuSecond bll_menuSecond = new BLL.T_NET_MenuSecond();
        CJJCH.BLL.T_NET_OLItem bll_olItem = new BLL.T_NET_OLItem();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                InitControl();
                InitRequest();
            }
        }
        private void InitControl()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"].Trim()))
            {
                CJJCH.Model.T_NET_MenuSecond model = new CJJCH.Model.T_NET_MenuSecond();
                model = bll_menuSecond.GetMenuSecondModel(Convert.ToInt32(Request.QueryString["secondid"].ToString()));
                if (model != null)
                {
                    this.lblSecondID.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["secondid"].Trim());
                    this.OnlineTop1.SecondId = Convert.ToInt32(this.lblSecondID.Text.Trim());
                    this.lblSecondName.Text = model.MenuName;
                    this.OnlineTop1.SecondName = this.lblSecondName.Text.Trim();
                    }
            
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }
            IList<CJJCH.Model.T_NET_OLItem> list = new List<CJJCH.Model.T_NET_OLItem>();
            list = bll_olItem.GetOLItem();
            int typeCount = list.Count;
            if (typeCount > 0)
            {
                for (int i = 0; i < typeCount; i++)
                {
                    dropNewsType.Items.Add(new ListItem(list[i].ItemName, list[i].ID.ToString()));
                }
                list = null;
            }
        }
        private void InitRequest()
        {
            if (Request.QueryString["keyword"] != null)
            {
                lblKeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].Trim());
                txtKeyword.Text = lblKeyword.Text;
            }
            if (Request.QueryString["orderkey"] != null)
            {
                lblOrderKey.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["orderkey"].Trim());
            }
            if (Request.QueryString["ordertype"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["ordertype"].Trim()))
            {
                lblOrderType.Text = Request.QueryString["ordertype"].Trim();
            }
            if (Request.QueryString["thirdid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["thirdid"].Trim()))
            {
                lblThirdID.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["thirdid"].Trim());
                dropNewsType.SelectedValue = lblThirdID.Text.Trim();
            }
            else
            {
                lblThirdID.Text = "0";
                dropNewsType.SelectedValue = lblThirdID.Text.Trim();
            }
            if (Request.QueryString["betime"] != null && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["betime"].Trim()))
            {
                lblBeTime.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["betime"].Trim());
                this.txtBegin.Text = lblBeTime.Text.Trim();
            }
            if (Request.QueryString["edtime"] != null && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["edtime"].Trim()))
            {
                lblEdTime.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["edtime"].Trim());
                this.txtEnd.Text = lblEdTime.Text.Trim();
            }
            if (Request.QueryString["curpage"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["curpage"].ToString()))
            {
                int curPage = int.Parse(Request.QueryString["curpage"].ToString());
                AspNetPager1.RecordCount = curPage * AspNetPager1.PageSize;
                AspNetPager1.CurrentPageIndex = curPage;
            }
            else
            {
                AspNetPager1.CurrentPageIndex = 1;
            }
        }
        private void RepeaterDataBinding()
        {
            string beginTime = "";
            try
            {
                beginTime = Convert.ToDateTime(lblBeTime.Text.Trim()).ToString();
            }
            catch { }
            string endTime = "";
            try
            {
                endTime = Convert.ToDateTime(lblEdTime.Text.Trim()).AddDays(1).ToString();
            }
            catch { }
            string strOrder = "";
            if (lblOrderKey.Text.Trim() != "")
            {
                strOrder += lblOrderKey.Text.Trim();
                if (lblOrderType.Text.Trim() == "1")
                {
                    strOrder += " desc,";
                }
                else
                {
                    strOrder += " asc,";
                }
            }
            int totalCount = 0;
            IList<CJJCH.Model.T_NET_Online> list = new List<CJJCH.Model.T_NET_Online>();
            string dataTitle = LTP.Common.StringClass.EncodeString(this.txtKeyword.Text.Trim());
            Guid TypeID = new Guid("00000000-0000-0000-0000-000000000000");
            if(lblThirdID.Text!="0"){
            TypeID = new Guid(this.lblThirdID.Text.Trim());
            }
            int secondid = Convert.ToInt32(this.lblSecondID.Text.ToString());
            list = bll_Online.SelOnline(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, dataTitle, TypeID, beginTime, endTime, ref totalCount, strOrder, secondid);
            AspNetPager1.RecordCount = totalCount > (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize ? totalCount : AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize;
            rptNews.DataSource = list;
            rptNews.DataBind();
            if (list.Count == 0)
                litMessage.Text = "<table width=100%><tr style=height:18px bgcolor=#FFFFFF><td align='center'><font color=red>对不起，系统没有检索出有关信息！<font></td></tr></table>";
            else
                litMessage.Text = "";
            AspNetPager1.CustomInfoHTML = "共有：<font color=\"blue\"><b>" + totalCount.ToString() + "</b></font>条记录";
            AspNetPager1.CustomInfoHTML += " <font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
            AspNetPager1.CustomInfoHTML += "/<font color=\"blue\"><b>" + AspNetPager1.PageCount.ToString() + "</b></font>";
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            lblKeyword.Text = LTP.Common.StringClass.EncodeString(txtKeyword.Text.Trim());
            lblBeTime.Text = LTP.Common.StringClass.EncodeString(txtBegin.Text.Trim());
            lblEdTime.Text = LTP.Common.StringClass.EncodeString(txtEnd.Text.Trim());
            this.lblThirdID.Text = LTP.Common.StringClass.EncodeString(dropNewsType.SelectedValue.ToString());

            AspNetPager1.CurrentPageIndex = 1;
        }
        protected void rptOnline_ItemDataBound(object sender, RepeaterItemEventArgs e) 
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((LinkButton)e.Item.FindControl("lnkItem")).Text = ((CJJCH.Model.T_NET_Online)e.Item.DataItem).Olitem.ItemName;
                ((LinkButton)e.Item.FindControl("lnkItem")).Attributes.Add("onclick", "ShowFinish('" + ((CJJCH.Model.T_NET_Online)e.Item.DataItem).ID.ToString() + "'); return false;");
                ((Label)e.Item.FindControl("lblCompanyName")).Text = ((CJJCH.Model.T_NET_Online)e.Item.DataItem).CompanyName;
                ((Label)e.Item.FindControl("lblContactName")).Text = ((CJJCH.Model.T_NET_Online)e.Item.DataItem).ContactName;
                if (((CJJCH.Model.T_NET_Online)e.Item.DataItem).OfficeTel != null)
                {
                    ((Label)e.Item.FindControl("lblTel")).Text = ((CJJCH.Model.T_NET_Online)e.Item.DataItem).OfficeTel;
                }
                else
                {
                    ((Label)e.Item.FindControl("lblTel")).Text=((CJJCH.Model.T_NET_Online)e.Item.DataItem).UserMobile;
                }
                ((Label)e.Item.FindControl("lblPeopleCount")).Text = ((CJJCH.Model.T_NET_Online)e.Item.DataItem).PeopleCount.ToString();
                ((Label)e.Item.FindControl("lblBuildDate")).Text = ((CJJCH.Model.T_NET_Online)e.Item.DataItem).BuildDate.ToString("yyyy/MM/dd");
            }
        }
    
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            RepeaterDataBinding();
        }

        protected void lnkItem_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = "T_NET_Online.ItemID";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }
        #region 抬头排序功能区

        protected void lnkcompanyName_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = "T_NET_Online.CompanyName";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void lnkContactName_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = "T_NET_Online.ContactName";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

      
        protected void lnkPeopleCount_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = "T_NET_Online.PeopleCount";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void lnkTime_Click(object sender, EventArgs e)
        {
            lblOrderKey.Text = "T_NET_Online.BuildDate";
            if (lblOrderType.Text == "0")
            {
                lblOrderType.Text = "1";
            }
            else
            {
                lblOrderType.Text = "0";
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        #endregion
    }
}